# -*- coding: utf-8 -*- 

import urllib2
import cookielib
from BeautifulSoup import BeautifulSoup,SoupStrainer
import time
import MySQLdb
import re
import m_product
import m_main


class Spider(object):#爬虫类基类
    def __init__(self):
        self.products=[]#返回后的产品组
        self.tasks=[]#返回后的任务组
        
class Spiders_Findcustomer_judgerecord(Spider):
    def __init__(self,netreturn):
        Spider.__init__(self)
        global RUNDATE#用来记录任务的时间
        contents=netreturn.contents
        print contents

        pagetotal=re.search(r'"maxPage":(.*?),',contents).group(1)#返回数据正确
        #print pagetotal
        if int(pagetotal)>0:
            ###########处理数据###############
            tlist=[]
            i=0
            for x in re.findall(r'"date":".*?"',contents):
                tlist.append(re.search(r'"date":"(.*?)"',x).group(1))
            for t in re.findall(r'"auction":{.*?},',contents):
                #print t
                customerJudgeRecord=m_product.CustomerJudgeRecord()
                customerJudgeRecord.buycomidity_id=re.search(r'"aucNumId":"(.*?)"',t).group(1)
                customerJudgeRecord.buycomidity_fullname=re.search(r'"title":"(.*?)"',t).group(1)
                customerJudgeRecord.buycomidity_sku=re.search(r'"sku":"(.*?)"',t).group(1)
                customerJudgeRecord.userid=netreturn.task.addin_data['userid']
                customerJudgeRecord.user_fullname=netreturn.task.addin_data['user_fullname']
                customerJudgeRecord.buytime=tlist[i]
                i+=1
                customerJudgeRecord._insertupdate='insert'
                self.products.append(customerJudgeRecord)
                #print customerJudgeRecord.buycomidity_fullname
                
            page_current=re.search(r'"currentPageNum":(.*?)[,}]',contents).group(1)
            if int(page_current)==1:
                for k in range(2,int(pagetotal),1):
                    curtask=netreturn.task
                    curtask.url='http://rate.taobao.com/member_rate.htm?callback=shop_rate_list&content=1&result=&from=rate&user_id='
                    curtask.url+=netreturn.task.addin_data['usercode']+'&identity=1&rater=3&direction=1&page='+str(k)
                    curtask.cookie=None
                    self.tasks.append(curtask)
            
            '''
            ###########添加任务###############
            if re.search(r'"currentPageNum":(.*?)},',contents).group(1)=='1':#返回第一页
                ''#添加任务
            time.sleep(3)#因访问淘宝太频繁，需要重启路由器，并把间隔时间增加0.1秒
            '''
        else:
            pass
        
class Spiders_Find_QQ_WeiBo_customer(Spider):
    def __init__(self,netreturn):
        Spider.__init__(self)
        contents=netreturn.contents
        print contents

            
   
        
                